package com.heima.kafka.simple.prodecer;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

/**
 * 生产者
 */
public class ProducerStart {
    public static void main(String[] args) {

        //1.生产者配置
        Properties properties = new Properties();

        //kafka连接点至
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.100.101:9092");

        //发送失败，失败的冲重试次数
        properties.put(ProducerConfig.RETRIES_CONFIG, 5);

        //消息key的序列化机器
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");

        //消息value序列化器

        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");

        //2.生产者对象
        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(properties);

        //封装发送消

        ProducerRecord<String, String> record = new ProducerRecord<>("itheima-topic", "100001", "hello kafka");

        //3.发送消息
        producer.send(record);

        //4.关闭消息通道
        producer.close();

    }
}
